package club.zhilin.guli.service.base.handler;

import club.zhilin.guli.common.base.result.CommonResult;
import club.zhilin.guli.common.base.result.CommonResultCodeEnum;
import club.zhilin.guli.service.base.exception.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.Serializable;
import java.util.Map;

/**
 * @author zhilin
 * @since 2022/1/12 17:52
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler implements Serializable {

    private static final long serialVersionUID = 7310228308772042884L;

    @ExceptionHandler(value = Exception.class)
    public CommonResult<String> globalException(Exception e) {
        e.printStackTrace();
        return CommonResult.failed(CommonResultCodeEnum.UNKNOWN_REASON, e.toString());
    }

    @ExceptionHandler(value = GlobalException.class)
    public CommonResult<Map<String, Integer>> globalException(GlobalException e) {
        log.error(ExceptionUtils.getMessage(e));
        return CommonResult.failed(e.getMessage(), e.getCode());
    }
}
